<?php
// +----------------------------------------------------------------------
// | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
// +----------------------------------------------------------------------
// | File name: AdminController.php
// +----------------------------------------------------------------------
// | Author: gxs
// +----------------------------------------------------------------------
// | Description: 后台管理员相关
// +----------------------------------------------------------------------
// | Others: 其他内容的说明
// +----------------------------------------------------------------------
// | History:
// +----------------------------------------------------------------------
// | 1.Date: 2018/12/28 Author: gxs Modification: 创建
// +----------------------------------------------------------------------

namespace App\Http\Controllers\Backend\Admin;

use App\Http\Requests\Backend\Admin\AdminRequest;
use App\Models\Admin as AdminModel;
use App\Http\Controllers\Controller;
use App\Models\AdminRole as AdminRoleModel;

class AdminController extends Controller
{
    /**
     * AdminController constructor.
     */
    public function __construct()
    {
        $this->middleware('auth:backend')->except('test');
    }

    /**
     * 检测管理员登录态
     *
     * @return mixed
     */
    public function check()
    {
        $check = auth('backend')->user();
        $id = $check->getAuthIdentifier();
        $user = AdminModel::with('roles')->find($id);
        return respond($user);
    }

    /**
     * 管理员列表
     *
     * @param AdminRequest $request
     * @return mixed
     */
    public function index(AdminRequest $request)
    {
        $params = $request->all();
        $page_size = $request->get('page_size', 0);

        $adminModel = new AdminModel();
        $adminModel->setConnection('backend');
        $list = $adminModel->getList($params, $page_size);
        return respond($list);
    }

    /**
     * 新增管理员
     *
     * @param AdminRequest $request
     * @return mixed
     */
    public function store(AdminRequest $request)
    {
        $role_id = $request->role;
        $adminRoleModel = new AdminRoleModel();
        $adminRoleModel->setConnection('backend');
        $role = $adminRoleModel->withCount('admins')->find($role_id);
        if ($role['admins_count'] >= $role['number'])
            return failed('该角色下账号超出数量限制');
        $model = new AdminModel();
        $model->account = $request->account;
        $model->password = $request->mobile;
        $model->mobile = $request->mobile;
        $model->email = $request->email;
        $model->post = $request->post;
        $model->role = $role_id;
        $model->save();
        return respond();
    }

    /**
     * 管理员修改
     *
     * @param AdminRequest $request
     * @param $id
     * @return mixed
     */
    public function update(AdminRequest $request, $id)
    {
        $params = $this->extractInputFromRules($request,$request->rules());
        AdminModel::where('id', $id)->update($params);
        return respond();
    }

    /**
     * 管理员删除
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $model = AdminModel::find($id);
        if (empty($model))
            return failed('账户不存在');
        if (AdminModel::SUPER_ADMIN_ACCOUNT == $model['account'])
            return failed('默认超级管理员,不可删除');
        AdminModel::destroy($id);
        return respond();
    }

    /**
     * 重置密码
     *
     * @param $id
     * @return mixed
     */
    public function reset($id)
    {
        $model = AdminModel::find($id);
        if (empty($model))
            return failed('账户不存在');
        $model->password = $model->mobile;
        $model->save();
        return respond();
    }

    /**
     * 修改密码
     *
     * @param AdminRequest $request
     * @param $id
     * @return mixed
     */
    public function password(AdminRequest $request, $id)
    {
        $model = AdminModel::find($id);
        if (empty($model))
            return failed('账户不存在');
        $password = $request->password;
        $check = [
            'account' => $model['account'],
            'password' => $password,
        ];
        if (!auth('backend')->validate($check))
            return failed('原密码错误');
        $password_confirm = $request->password_confirm;
        $model->password = $password_confirm;
        $model->save();
        return respond();
    }

    public function test()
    {
        return 'test';
    }
}